﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace WebCycle
{
    public partial class WebCycle : Form
    {
        internal ScreensaverConfiguration Config = new ScreensaverConfiguration();

        private int _currentUrl;
        private static bool previewMode = false;

        public WebCycle()
        {
            InitializeComponent();
        }

        static void Main()
        {
            // Set the unhandled exception mode to force all Windows Forms errors to go through
            // our handler.
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            // Add the event handler for handling non-UI thread exceptions to the event. 
            AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler(HandleException);

            string[] cmdList = Environment.CommandLine.Split(' ');

            if (cmdList.Length >= 2)
            {
                if (cmdList[1].IndexOf("/c") >= 0)  // Configuration mode
                {
                    //Spawn Options Window
                    Application.Run(new Settings());
                    return;
                }

                if (cmdList[1] == "/p") 	// Preview mode
                {
                    previewMode = true;
                }

                // -s parameter means to run in full-screen mode
            }

            //Spawn Parent Window
            if (!previewMode)
            {
                Application.Run(new WebCycle());
            }
        }

        private static void HandleException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = (Exception)e.ExceptionObject;
                string errorMsg = "An application error occurred. Please contact the adminstrator " +
                    "with the following information:\n\n";

                // Since we can't prevent the app from terminating, log this to the event log.
                if (!EventLog.SourceExists("WebCycle"))
                {
                    EventLog.CreateEventSource("WebCycle", "Application");
                }

                // Create an EventLog instance and assign its source.
                EventLog myLog = new EventLog();
                myLog.Source = "WebCycle";
                myLog.WriteEntry(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace);
            }
            catch (Exception exc)
            {
                Application.Exit();
            }
        }

        private void MainForm_Load(object sender, System.EventArgs e)
        {
            //Spawn Child Windows
            for (int i = Screen.AllScreens.GetLowerBound(0);
                i <= Screen.AllScreens.GetUpperBound(0);
                i++)
            {
                ScreensaverThreadData data = new ScreensaverThreadData();
                data.Screen = i;
                data.Parent = this;

                ScreensaverThread screensaverThread = new ScreensaverThread();
                Thread oThread = new Thread(screensaverThread.Start);
                oThread.SetApartmentState(ApartmentState.STA);
                oThread.Start(data);
            }
        }

        internal void Bail()
        {
            Application.Exit();
        }

        internal string NextUrl()
        {
            string[] urls = Config.Urls.ToArray();

            if (_currentUrl == urls.Length)
                _currentUrl = 0;

            return urls[_currentUrl++].Trim();
        }

        private void MainForm_Click(object sender, EventArgs e)
        {
            Bail();
        }
    }
}
